package com.plant.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plant.entity.PgcTopic;
import com.plant.service.TopicService;

/**
 *  @author 植物先生--李琦    PC端---话题管理（新建话题，删除话题，查看话题，修改话题）
 * 			---涉及的表：表1：
 * 			 @Table(name="pgc_topic",catalog="mr_plants")
 * 			  --secret	由 随机的字符串 和 密匙（token） 组成进行md5加密组成
				---	pust_val	随机生成的字符串。可以是时间戳或乱码字符
 * 
 */
@Controller                                                                
@RequestMapping(value = "/topic")                                                
public class TopicAction {

    @Resource(name = "topicServiceImpl")
    private TopicService topicService;

    
	@RequestMapping(value = "newtopic")//---1.PC端---新建话题
    @ResponseBody                                             
    public Map<Object,Object> newtopic(PgcTopic pt,String secret,String pust_val,HttpServletRequest request,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pt.getUserId()==null||pt.getTitle()==null||pt.getContent()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
    	}else{
		    Md md=new Md();
		    String mds=md.md5calc(pust_val+"flower_duigu2016");
		    if(secret.endsWith(mds)){
				    SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    String t=sim.format(new Date());
		    		GetIp getip=new GetIp();
		    		String ip=getip.getIp2(request);
		    		pt.setIp(ip);
		    		pt.setPv(0);
		    		pt.setCreateTime(Timestamp.valueOf(t));
		    		boolean b=topicService.addtopic(pt);//---新建话题
		    		if(b){
		    			map.put("message", "新建成功");
		    			map.put("state", "1");
		    			return map; 
		    		}else{
		    			map.put("message", "新建失败");
		    			map.put("state", "0");
		    			return map; 
		    		}
		    }else{
	    		map.put("message", "秘钥错误");
	    		map.put("state", "0");
	    		return map;
		    }
    	}
    }
	
	
	@RequestMapping(value = "uptopic")//---2.PC端---修改话题
	@ResponseBody                                             
	public Map<Object,Object> uparticle(PgcTopic pt,String secret,String pust_val,HttpServletRequest request,HttpServletResponse response) {
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pt.getUserId()==null||pt.getTitle()==null||pt.getContent()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
    	}else{
		    Md md=new Md();
		    String mds=md.md5calc(pust_val+"flower_duigu2016");
		    if(secret.endsWith(mds)){
		    			PgcTopic p=(PgcTopic) topicService.oneinf(pt);
		    			pt.setCreateTime(p.getCreateTime());
		    			pt.setIp(p.getIp());
						pt.setPv(555);
						boolean b=topicService.uptopic(pt);//修改话题
						if(b){
							map.put("state", "1");
							map.put("message", "修改成功");
							return map;  
						}else{
							map.put("state", "0");
							map.put("message", "修改失败");
							return map;  
						}
		    }else{
	    		map.put("message", "秘钥错误");
	    		map.put("state", "0");
	    		return map;
		    }
    	}
	}
	
	
	
	@RequestMapping(value = "pconetopic")//---3.PC端--- 查看单个话题详情
	@ResponseBody                                             
	public Map<Object,Object> onetopic(PgcTopic pt,String secret,String pust_val,String time,Integer page,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pt.getId()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
						List<Object> list=new ArrayList<Object>();
						list=topicService.onetopic(pt);
						map.put("onetopic", list);	
						map.put("state", "1");
						map.put("message", "查看话题成功");
						return map; 
		  	  }else{
	    			map.put("message", "秘钥错误");
	    			map.put("state", "0");
	    			return map;
		    }
    	}
	}
	@RequestMapping(value = "listtopic")//---4.PC端--显示话题列表 每页显示20条数据
	@ResponseBody                                             
	public Map<Object,Object> listarticle(PgcTopic pt,String secret,String pust_val,String time,Integer page,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pt.getUserId()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
						List<Object> list=new ArrayList<Object>();
						
						list=topicService.listtopic(pt,page, time);
						long count=topicService.topiccount(pt);//---总条数
		    			int pagecount=(int) ((count-1)/20+1);  //总共多少页
		    			
		    			map.put("pagecount", pagecount);
						map.put("list", list);	
						map.put("count", count);	
						map.put("state", "1");
						map.put("message", "查看话题成功");
						return map; 
		  	  }else{
	    			map.put("message", "秘钥错误");
	    			map.put("state", "0");
	    			return map;
		    }
    	}
	}
	
	
	@RequestMapping(value = "deltopic")//---5.PC端--删除话题
	@ResponseBody                                             
	public Map<Object,Object> delarticle(PgcTopic pt,String secret,String pust_val,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(secret==null||pust_val==null||pt.getId()==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
//							pt.setId(11);
							boolean b=topicService.deltopic(pt);
							if(b){
								map.put("state", "1");
								map.put("message", "删除文章成功");
								return map;
							}else{
								map.put("state", "0");
								map.put("message", "删除文章失败");
								return map;
							}
				  }else{
			    			map.put("message", "秘钥错误");
			    			map.put("state", "0");
			    			return map;
				 }
    	}
	}
    

}
